<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateOrderBundlesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('order_bundles', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger("bundle_id");
            $table->string("name")->comment("组合名称");
            $table->unsignedBigInteger("order_id");
            $table->decimal("amount")->nullable()->comment("组合价格");
            $table->dateTime("rental_out_at")->nullable()->comment("出库时间");
            $table->dateTime("return_at")->nullable()->comment("入库时间");
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('order_bundle_items', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger("order_bundle_id");
            $table->string("name")->comment("资产名称");
            $table->unsignedBigInteger("rentalable_id");
            $table->string("rentalable_type");
            $table->integer("quantity")->default(1)->comment("数量");
            $table->decimal("unit_price")->default(1)->comment("单价");
            $table->decimal("amount")->default(0)->comment("价格");
            $table->dateTime("rental_out_at")->nullable()->comment("出库时间");
            $table->dateTime("return_at")->nullable()->comment("入库时间");
            $table->timestamps();
            $table->softDeletes();
        });

        DB::select(DB::raw("alter table order_items alter column order_id type bigint using order_id::bigint;"));

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('order_bundle_items');
        Schema::dropIfExists('order_bundles');
        Schema::table("order_items", function(Blueprint $table){
            $table->string('order_id')->change();
        });

    }
}
